//
//  RegisterViewController.m
//  Ev_Master
//
//  Created by Evan on 2019/10/31.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"
#import "taskUserService.h"
#import "LoginSuccessViewController.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *txtPhone;
@property (weak, nonatomic) IBOutlet UITextField *txtPwd;
@property (weak, nonatomic) IBOutlet UITextField *txtCode;
@property (weak, nonatomic) IBOutlet UITextField *txtInvCode;
@property (weak, nonatomic) IBOutlet UIButton *btnSendCode;


@property(nonatomic, assign) NSInteger currentCountDown;
@property(nonatomic, strong) NSTimer *timer;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setUpUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [self setHiddenNavigation:YES];
    [self hideHeadView:YES];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

- (IBAction)btnLogin:(id)sender {
    LoginViewController *vc = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)btnRegisterClick:(id)sender {
    if ([self.txtPhone.text  isEqualToString:@""] || [self.txtInvCode.text  isEqualToString:@""] ||
        [self.txtPwd.text  isEqualToString:@""] || [self.txtCode.text  isEqualToString:@""]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的注册信息"];
        return;
    }
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:self.txtPhone.text forKey:@"username"];
    [dict setValue:self.txtPwd.text forKey:@"password"];
    [dict setValue:self.txtCode.text forKey:@"verifyCode"];
    [dict setValue:self.txtInvCode.text forKey:@"RefereeUser"];
    [dict setValue:self.txtInvCode.text forKey:@"ParentUser"];
    [dict setValue:@"true" forKey:@"agree"];
    
    [taskUserService userRegister:dict success:^(modelUserData * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        //注册成功
        [DKProgressHUD showSuccessWithStatus:@"注册成功"];
        LoginViewController *vc = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        //注册失败
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}



- (void)setUpUI{
    
    CAGradientLayer *gl2 = [CAGradientLayer layer];
    gl2.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
    gl2.startPoint = CGPointMake(0, 0);
    gl2.endPoint = CGPointMake(1, 1);
    gl2.colors = @[(__bridge id)[UIColor colorWithRed:51/255.0 green:224/255.0 blue:240/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:72/255.0 green:146/255.0 blue:236/255.0 alpha:1.0].CGColor];
    gl2.locations = @[@(0.0),@(1.0)];
    
    [self.view.layer insertSublayer:gl2 atIndex:0];
    
    self.txtPhone.attributedPlaceholder = [self attrStr:self.txtPhone strPlaceholder:@"请输入手机号"];
    self.txtPwd.attributedPlaceholder = [self attrStr:self.txtPwd strPlaceholder:@"请输入密码"];
    self.txtCode.attributedPlaceholder = [self attrStr:self.txtCode strPlaceholder:@"请输入验证码"];
    self.txtInvCode.attributedPlaceholder = [self attrStr:self.txtInvCode strPlaceholder:@"请输入邀请码"];
}


- (NSAttributedString*) attrStr:(UITextField*)txtField strPlaceholder:(NSString*)str{
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:str attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:txtField.font}];
    return attrString;
}

- (IBAction)btnSendCodeClick:(id)sender {
    
    if ([self.txtPhone.text  isEqualToString:@""]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:@"用户注册" forKey:@"usage"];
    [taskUserService sendSMS:dict success:^(BaseModel * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showSuccessWithStatus:response.message];
        
        [self startCount];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


#pragma mark - 倒计时
- (void)startCount
{
    /**
     *  添加定时器
     */
    self.currentCountDown = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
}

- (void)countDown{
    
    if (self.currentCountDown >0) {
        //设置界面的按钮显示 根据自己需求设置
        [self.btnSendCode setTitle:[NSString stringWithFormat:@"(%ld)重新获取",(long)self.currentCountDown] forState:UIControlStateNormal];
        self.btnSendCode.enabled = NO;
        self.currentCountDown -= 1;
    }else{
        [self removeTimer];
        self.btnSendCode.enabled = YES;
    }
    
}

/**
 *  移除定时器
 */
- (void)removeTimer
{
    self.currentCountDown = 0;
    [self setCaptchaEnable:YES];
    [self.timer invalidate];
    //    self.timer = nil;
}



//因为iOS 7下 按钮 enabled= NO, 不能设置文字
#pragma mark - 设置按钮状态
- (void)setCaptchaEnable:(BOOL)enabled{
    //可以按
    if (enabled) {
        self.btnSendCode.userInteractionEnabled = YES;
        [self.btnSendCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }else{
        self.btnSendCode.userInteractionEnabled = NO;
    }
}

@end
